﻿using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using FindMyChamp.Helpers;
using System.Windows.Data;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Tasks;
using CodePlex.WindowsPhone;

namespace FindMyChamp
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.DataContext = App.ViewModel;
            App.ViewModel.DistanceChanged += (s, e) =>
            {
                MyViewSource1.View.Refresh();
                MyViewSource2.View.Refresh();
                map.Center = GeoData.Location;
            };
            this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
            this.Loaded += (s, e) =>
            {
                App.ViewModel.LoadChampsComplete += ViewModel_LoadChampsComplete;
                App.ViewModel.LoadChamps();

                // determine whether to ask the user to review the application.
                MarketplaceReviewRequestor requestor = new MarketplaceReviewRequestor();
            };
        }

        void ViewModel_LoadChampsComplete(object sender, ViewModel.LoadChampsCompleteEventArgs e)
        {
            GeoData.RaisePositionChanged();
            if (e.Error == null)
                return;
            var _Message = string.Format("Please ensure your network is available.\r\n{0}", e.Error.Message);
            MessageBox.Show(_Message, "Error fetching champs", MessageBoxButton.OK);
        }

        // tilted phone
        private void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            ApplicationBar.IsVisible = e.Orientation == (e.Orientation & (PageOrientation.Portrait | PageOrientation.PortraitDown | PageOrientation.PortraitUp));
            map.Visibility = ApplicationBar.IsVisible ? Visibility.Collapsed : Visibility.Visible;
            pivot.Visibility = ApplicationBar.IsVisible ? Visibility.Visible : Visibility.Collapsed;
        }

        // tapped champ in list
        private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox _ListBox = (ListBox)sender;
            var _Item = _ListBox.SelectedItem as WPChamp;
            if (_Item == null)
                return;
            _ListBox.SelectedIndex = -1;
            ShowChamp(_Item);
        }

        // tapped champ in map
        private void Pushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var _Item = (sender as Pushpin).DataContext as WPChamp;
            if (_Item == null)
                return;
            ShowChamp(_Item);
        }

        // nav to show champ detail
        private void ShowChamp(WPChamp champ)
        {
            var _Url = string.Format("/Views/DetailsPage.xaml?selectedItem={0}", champ.ID);
            var _Uri = new Uri(_Url, UriKind.Relative);
            NavigationService.Navigate(_Uri);
        }

        #region ApplicationBar

        private void AboutMenuItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AboutPage.xaml", UriKind.Relative));
        }

        private void RateThisMenuItem_Click(object sender, EventArgs e)
        {
            new MarketplaceReviewTask().Show();
        }

        private void RefreshMenuItem_Click(object sender, EventArgs e)
        {
            App.ViewModel.LoadChamps();
        }

        private void PrivacyMenuItem_Click(object sender, EventArgs e)
        {
            var _Url = "/Views/Settings.xaml";
            var _Uri = new Uri(_Url, UriKind.Relative);
            NavigationService.Navigate(_Uri);
        }

        private void ShareThisMenuItem_Click(object sender, EventArgs e)
        {
            new ShareLinkTask
            {
                Title = "Find My Champ",
                LinkUri = new Uri("http://www.windowsphone.com/en-US/apps/a84f1740-0047-4408-b317-c2db01a70fb4", UriKind.Absolute),
                Message = "App to help find Windows Phone Champs.",
            }.Show();
        }

        #endregion

        private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // refresh view
            MyViewSource1.View.Refresh();
        }

        private void MyViewSource1_Filter(object sender, FilterEventArgs e)
        {
            // filter based on textbox
            e.Accepted = (e.Item as WPChamp).FullName.ToLower().Contains(SearchTextBox.Text.ToLower());
        }

        // filter for local list
        private void MyViewSource2_Filter(object sender, System.Windows.Data.FilterEventArgs e)
        {
            e.Accepted = (e.Item as WPChamp).IsLocal;
        }
    }
}